package com.lanou.musicproject.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;

import com.lanou.musicproject.download.DownloadProgressListener;
import com.lanou.musicproject.download.FileDownloader;
import com.lanou.musicproject.net.NetDataWhat;
import com.lanou.musicproject.utils.MusicPlayer;
import com.lanou.musicproject.utils.MyConfig;

import java.io.File;

/**
 * Created by ztq on 15/10/21.
 * <p/>
 * 包含下载方法
 */
public class BaseActivity extends Activity implements NetDataWhat {

    public DownloadTask task;
    private Handler handler = new UIHandler();
    private ProgressBar progressBar;
    private Context context;
    private MusicPlayer musicPlayer;

    private static final int PROCESSING = 1;
    private static final int FAILURE = -1;


    //下载的线程&&下载进度条
    private final class UIHandler extends Handler {
        public void handleMessage(Message msg) {
            switch (msg.what) {
//                case PROCESSING: // 更新进度
//                    progressBar.setProgress(msg.getData().getInt("size"));
//                    float num = (float) progressBar.getProgress()
//                            / (float) progressBar.getMax();
//                    int result = (int) (num * 100); // 计算进度
//                    //resultView.setText(result + "%");
//                    if (progressBar.getProgress() == progressBar.getMax()) { // 下载完成
//                        Toast.makeText(getApplicationContext(), R.string.success,
//                                Toast.LENGTH_LONG).show();
//                    }
//                    break;
//                case FAILURE: // 下载失败
//                    Toast.makeText(getApplicationContext(), R.string.error,
//                            Toast.LENGTH_LONG).show();
//                    break;
            }
        }

    }

    /**
     * 下载方法
     */
    public void download(String path, File savDir) {

        path = MyConfig.getSharePreStr(this, "link", "url");

        task = new DownloadTask(path, savDir);
        new Thread(task).start();

        Log.i("--------base-----------", "path = " + path);
        Log.i("--------dir-----------", "dir = " + savDir);
    }


    /**
     * 在线播放方法
     **/

    public void playOnline(String path) {

        path = MyConfig.getSharePreStr(BaseActivity.this, "link", "url");
        musicPlayer.playUrl(path);

    }

    /**
     * 播放方法
     **/

    public void play() {
        musicPlayer.play();
    }





    /**
     * UI控件画面的重绘(更新)是由主线程负责处理的，如果在子线程中更新UI控件的值，更新后的值不会重绘到屏幕上
     * 一定要在主线程里更新UI控件的值，这样才能在屏幕上显示出来，不能在子线程中更新UI控件的值
     * <p/>
     * 下载任务的线程 内部类
     */
    private final class DownloadTask implements Runnable {

        /**
         * path就是下载链接
         * <p/>
         * path拼接成下载地址即可
         * <p/>
         * sdcard/dowmload
         */
//        private String path;
        private File saveDir;
        private FileDownloader loader;


        Intent intent = getIntent();
        String path = intent.getStringExtra("show_link");


        //构造方法
        public DownloadTask(String path, File saveDir) {
            this.path = path;
            this.saveDir = saveDir;
        }

        /**
         * 退出下载
         */
        public void exit() {
            if (loader != null)
                loader.exit();
        }


        //下载进度条
        DownloadProgressListener downloadProgressListener = new DownloadProgressListener() {
            @Override
            public void onDownloadSize(int size) {
                Message msg = new Message();
                msg.what = PROCESSING;
                msg.getData().putInt("size", size);
                handler.sendMessage(msg);
            }
        };


        //线程执行方法
        //进度条
        public void run() {
            // 实例化一个文件下载器

            if(path != null) {
                loader = new FileDownloader(getApplicationContext(), path,
                        saveDir, 3);
            }

            Log.i("--------run-----------", "path = " + path);
            Log.i("--------run-----------", "dir = " + saveDir);

//                // 设置进度条最大值
////                progressBar.setMax(loader.getFileSize());
            try {
                loader.download(downloadProgressListener);
            } catch (Exception e) {
                e.printStackTrace();
                handler.sendMessage(handler.obtainMessage(FAILURE)); // 发送一条空消息对象
            }
        }
    }

}
